fix: symmetric N/* handling across strands in compute_pwm - #40
Merged
Conversation
compute_pwm() scored N (and the * wildcard) inconsistently between strands: integrate_energy() and integrate_energy_max() used the column's average log-probability (get_avg_log_prob) on the forward strand but a flat log(0.25) (c_log_quarter) on the reverse strand. With bidirect=TRUE this broke strand symmetry - a window and its reverse-complement scored differently (~1.6 nats) whenever an N fell on an informative position. Use get_avg_log_prob() on both strands, matching the forward branch and the other likelihood routines (max_like_match, integrate_like_seg). Only affects sequences containing N/*. Claude-Session: https://claude.ai/code/session_01PK3qefBGDoBwd9w5226FEq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Reported by a user via Tamar: how does
compute_pwmtreatN, and why does the forward branch useget_avg_log_prob()while the reverse branch useslog(0.25)?Confirmed - it's a real strand asymmetry. In
src/DnaPSSM.cpp, the two functionscompute_pwmuses treatN/*differently on the two strands:p->get_avg_log_prob()- the column's mean log-probability,(log pA + log pC + log pG + log pT)/4.c_log_quarter=log(0.25), a flat constant.Since a normalized column sums to 1, these are equal only at a uniform column; at informative columns
mean(log p) < log(0.25), so the forward strand penalizes anNmore than the reverse strand.Impact
For a bidirectional motif, a window and its reverse-complement must score identically. They do - until an
Nlands on an informative position:Only affects sequences containing
N/*withbidirect = TRUE.Fix
Use
get_avg_log_prob()on both strands inintegrate_energyandintegrate_energy_max. That restores strand symmetry and matches the convention the symmetric likelihood routines (max_like_match,integrate_like_seg) already use on both strands - soavgis the codebase's intended convention and the reversec_log_quarterwas the outlier.Out of scope (left as-is):
integrate_like/update_like_veccarry a different asymmetry (forwardlog(0.25), reverseavg) but are learning/likelihood internals, not thecompute_pwmenergy path.like_thresh_matchis intentionally symmetric (log(0.25)on both). The identical fix is being applied to misha'sDnaPSSM.cpp(same shared code) in a companion PR.Test
Added a strand-symmetry test: a motif-length window with an
Non an informative column must score the same as its reverse-complement, for bothmaxandlogSumExp. Fails before, passes after. Full test suite green.https://claude.ai/code/session_01PK3qefBGDoBwd9w5226FEq